package com.karst.essentials.chapter06
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;

	internal class OrbitExample3 extends InteractionTemplate
	{
		private var rotX : Number = .3 ;
		private var rotY : Number = .3 ;
		private var camPicth : Number = 90 ;
		private var camYaw : Number = 270 ;
		
		private var isOrbiting : Boolean = false ;
		private var previousMouseX : Number = 0 ;
		private var previousMouseY : Number = 0 ;
		private var easePitch : Number = 90 ;
		private var easeYaw : Number = 270 ;
		private var easeOut : Number = .1 ;
		
		
		public function OrbitExample3()
		{
			super() ;
			addEvents() ;
		}

		private function addEvents() : void
		{
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler ) ;
			addEventListener( MouseEvent.MOUSE_MOVE, mouseMoveHandler ) ;
			addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler ) ;
		}
		
		private function mouseDownHandler( event : MouseEvent ) : void
		{
			isOrbiting = true ;
			previousMouseX = event.stageX ;
			previousMouseY = event.stageY ;
		}
		
		private function mouseMoveHandler( event : MouseEvent ) : void
		{
			if( isOrbiting )
			{
				camYaw += event.stageX - previousMouseX ;
				camPicth += event.stageY - previousMouseY ;
				previousMouseX = event.stageX ;
				previousMouseY = event.stageY ;
			}
		}
		
		private function mouseUpHandler( event : MouseEvent ) : void
		{
			isOrbiting = false ;
		}

		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick( event ) ;
			
//			camPicth += (( mouseY - height * .5 ) * rotX + 90 - camPicth ) * easeOut ;
//			camYaw += (( mouseX - width * .5  ) * rotY + 270 - camYaw ) * easeOut ;
			camera.orbit( camPicth, camYaw ) ;
		}
	}
	
}